package com.crl.rcdp.sys.service;

import com.cars.util.bean.BasePage;
import com.cars.util.list.ListUtils;
import com.cars.util.string.StringUtil;
import com.crl.rcdp.sys.dao.SysResourceDao;
import com.crl.rcdp.sys.dao.SysUserDao;
import com.crl.rcdp.sys.dao.SysUserLoginDao;
import com.crl.rcdp.sys.dao.SysUserRoleDao;
import com.crl.rcdp.sys.dto.SysUserDTO;
import com.crl.rcdp.sys.model.SysResource;
import com.crl.rcdp.sys.model.SysUser;
import com.crl.rcdp.sys.model.SysUserLogin;
import com.crl.rcdp.sys.model.SysUserRole;
import com.crl.rcdp.sys.vo.SysUserSearchVO;
import com.crl.redis.RedisKey;
import com.crl.redis.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/6/22 11:11
 */
@Service
public class SysUserService {
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;
    @Autowired
    private SysResourceDao sysResourceDao;
    @Autowired
    private SysUserLoginDao sysUserLoginDao;
    @Autowired
    private RedisUtil redisUtil;
    /**
     * 获取所有角色
     */
    public BasePage<List<SysUserDTO>> page(SysUserSearchVO sysUserSearchVO) {
        BasePage<List<SysUserDTO>> basePage = new BasePage();
        int count = sysUserDao.count(sysUserSearchVO);
        basePage.setTotal(count);
        List<SysUser> list = sysUserDao.list(sysUserSearchVO);
        List<SysUserDTO> listResult = ListUtils.copyList(list, SysUserDTO.class);
        basePage.setData(listResult);
        return basePage;
    }

    /**
     * 新增
     *
     * @param sysUserDTO
     * @return
     */
    public int add(SysUserDTO sysUserDTO) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDTO, sysUser);
        int userId = sysUserDao.add(sysUser);
        ///新增角色表
        String roleIds = sysUserDTO.getRoleIds();
        String[] roleIdArr = roleIds.split("@");
        for (String roleId : roleIdArr) {
            if (StringUtil.isNotNullOrEmpty(roleId)) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(userId);
                sysUserRole.setRoleId(Integer.parseInt(roleId));
                sysUserRoleDao.add(sysUserRole);
            }
        }
        //新增用户登录信息
        SysUserLogin sysUserLogin = new SysUserLogin();
        String password = new BCryptPasswordEncoder(10).encode("123456"); //默认密码
        sysUserLogin.setPassword(password);
        sysUserLogin.setUserId(userId);
        sysUserLogin.setUsername(sysUserDTO.getUsername());
        sysUserLogin.setMobile(sysUserDTO.getMobile());
        sysUserLoginDao.add(sysUserLogin);
        return 1;
    }

    /**
     * 修改
     *
     * @param sysUserDTO
     * @return
     */
    public int update(SysUserDTO sysUserDTO) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDTO, sysUser);
        sysUserDao.update(sysUser);
        sysUserRoleDao.deleteByUserId(sysUser.getId());
        String roleIds = sysUserDTO.getRoleIds();
        String[] roleIdArr = roleIds.split("@");
        for (String roleId : roleIdArr) {
            if (StringUtil.isNotNullOrEmpty(roleId)) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(sysUser.getId());
                sysUserRole.setRoleId(Integer.parseInt(roleId));
                sysUserRoleDao.add(sysUserRole);
            }
        }
        //修改对应的redis的值
        redisUtil.del(RedisKey.USER_ROLES_KEY + sysUserDTO.getId());
        redisUtil.set(RedisKey.USER_ROLES_KEY + sysUserDTO.getId(), this.listUseRoles(sysUserDTO.getId()));
        return 1;
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    public int delete(int id) {
        return sysUserDao.delete(id);
    }

    public SysUserDTO get(int id) {
        SysUser sysUser = sysUserDao.get(id);
        SysUserDTO sysUserDTO = new SysUserDTO();
        BeanUtils.copyProperties(sysUser, sysUserDTO);
        //获取用户对应的角色
        List<SysUserRole> listUserRole = sysUserRoleDao.listByUserId(id);
        StringBuffer sb = new StringBuffer();
        for (SysUserRole sysUserRole : listUserRole) {
            sb.append(sysUserRole.getRoleId()).append("@");
        }
        sysUserDTO.setRoleIds(sb.toString());
        return sysUserDTO;
    }

    /**
     * 获取单个用户的登录信息
     *
     * @param id
     * @return
     */
    public SysUserDTO getUserLogin(int id) {
        SysUser sysUser = sysUserDao.get(id);
        SysUserDTO sysUserDTO = new SysUserDTO();
        BeanUtils.copyProperties(sysUser, sysUserDTO);
        //获取用户对应的角色
        List<SysUserRole> listUserRole = sysUserRoleDao.listByUserId(id);
        StringBuffer sb = new StringBuffer();
        for (SysUserRole sysUserRole : listUserRole) {
            sb.append(sysUserRole.getRoleId()).append("@");
        }
        //获取用户对应的资源
        List<SysResource> listUserResource = sysResourceDao.listByUserd(id);
        StringBuffer sb2 = new StringBuffer();// 用户资源代码
        for (SysResource sysResource : listUserResource) {
            sb2.append(sysResource.getResourceCode()).append("@");
        }
        sysUserDTO.setRoleIds(sb.toString()); //用户对应的角色
        sysUserDTO.setUserResources(sb2.toString());//
        return sysUserDTO;
    }

    /**
     * 根据用户id获取对应的角色列表
     *
     * @param userId
     * @return
     */
    public List<SysUserRole> listUseRoles(int userId) {
        return sysUserRoleDao.listByUserId(userId);
    }

}
